//
//  topMovieMesViewController.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/27.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "topMovieMesViewController.h"
#import "common.h"
#import "DataService.h"
#import "headerModal.h"
#import "headerCollectionViewCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import "tableModal.h"


@interface topMovieMesViewController ()

@end

@implementation topMovieMesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createTableView];
    [self _createHeaderView];
    [self _loadHeaderData];
    [self _loadTableData];
  }

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - _createTableView
- (void)_createHeaderView{
    self.view.backgroundColor = [UIColor blackColor];
   UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, 250)];
    tableHeaderView.backgroundColor = [UIColor clearColor];
   _commentTableView.tableHeaderView = tableHeaderView;
    
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSArray *array = [bundle loadNibNamed:@"headerMovieView" owner:self options:nil];
    _headerMovieView = [array lastObject];
    
    _headerMovieView.frame = CGRectMake(0, 0, Kwidth, 200);
    _headerMovieView.backgroundColor = [UIColor blackColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0,
  //  layout.minimumLineSpacing = 0;
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake((Kwidth-3)/4, 55);
    _headerCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 195, Kwidth, 55) collectionViewLayout:layout];
    _headerCollectionView.delegate = self;
    _headerCollectionView.dataSource = self;
    [_headerCollectionView registerClass:[headerCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    [tableHeaderView addSubview:_headerMovieView];
    [tableHeaderView addSubview:_headerCollectionView];
    
}

- (void)_createTableView{
    self.title = @"电影详情";
    
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.translucent = YES;
    _commentTableView = [[movieCommentTableView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, Kheight) style:UITableViewStylePlain];
    _commentTableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_commentTableView];

    
    
}

#pragma mark - loadData
- (void)_loadHeaderData{
    imagesArray = [[NSArray alloc] init];
    NSDictionary *dic = [DataService initWitnJsionFileName:movieDetialFile];
    headerModal *modal = [[headerModal alloc] init];
    modal.content = dic[@"content"];
    modal.image = dic[@"image"];
    modal.titleCn = dic[@"titleCn"];
    modal.directors = dic[@"directors"];
    modal.actors = dic[@"actors"];
    modal.type = dic[@"type"];
    modal.releaseDic = dic[@"release"];
    modal.year = dic[@"year"];
   // modal.videos = dic[@"videos"];
    imagesArray = dic[@"videos"];
    
    _headerMovieView.modal = modal;
    
}

- (void)_loadTableData{
    tableModalArray = [[NSMutableArray alloc] init];
    NSDictionary *dic = [DataService initWitnJsionFileName:movieCommentFile];
    NSArray *listArray = [dic objectForKey:@"list"];
    for (NSDictionary *list in listArray) {
        tableModal *modal = [[tableModal alloc] init];
        [modal setValuesForKeysWithDictionary:list];
        [tableModalArray addObject:modal];
    }
    _commentTableView.modalArray = tableModalArray;
    
}

#pragma mark- delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return imagesArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    headerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
    cell.urlStr = imagesArray[indexPath.row][@"image"];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    MPMoviePlayerViewController *ctrl = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:imagesArray[indexPath.row][@"url"]]];
    
    //将视频播放器弹出
    
    [self presentMoviePlayerViewControllerAnimated:ctrl];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
